#include "Game Engine/Header/Player.h"
#include "Game Engine/Header/Camera.h"

Player::Player()
{
	mPosition	= D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
	mVelocity	= D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
}

Player::~Player()
{
}

D3DXVECTOR3& Player::GetPosition()
{
	mPosition = BT2DX_VECTOR3( mCharacter->getPosition() );

	return mPosition;
}

D3DXVECTOR3& Player::GetVelocity()
{
	mVelocity.y *= 0.6f;
	mVelocity.x *= 0.6f;
	mVelocity.z *= 0.6f;

	return mVelocity;
}

void Player::SetPosition( D3DXVECTOR3 pos )
{
	mPosition = pos;

	mCharacter->warp( BTVector3( mPosition ) );
}

void Player::SetVelocity( D3DXVECTOR3 vel )
{
	mVelocity = vel;
}

void Player::Strafe( float d )
{
	D3DXVECTOR3 r = GetCamera().GetCameraRight();
	mVelocity += d * GetCamera().GetCameraRight();
}

void Player::Walk( float d )
{
	mVelocity += d * GetCamera().GetCameraLook();
	//mVelocity.x += d * GetCamera().GetCameraLook().x;
	//mVelocity.z += d * GetCamera().GetCameraLook().z;
}

void Player::Init( Mesh* mesh, Object::Shape shape, bool dynamic )
{
	Object::Init( mesh, shape, dynamic );

	mMesh = mesh;
	//mBody = Object::GetBody();

	D3DXVECTOR3 min;
	D3DXVECTOR3 max;

	Object::GetAABB( min, max );

	float height = abs( max.y - min.y );
	float width = abs( max.x - min.x );
	float depth = abs( max.z - min.z );

	btTransform startTransform;
	startTransform.setIdentity ();
	//startTransform.setOrigin (btVector3(0.0, 4.0, 0.0));
	startTransform.setOrigin (btVector3(0.0f, 0.0f, 0.0f));


	mGhostObject = new btPairCachingGhostObject();
	mGhostObject->setWorldTransform(startTransform);
	btScalar characterHeight = height/2 + 0.04f;
	btScalar characterWidth = width/2;
	btScalar characterDepth = depth/2;
	btConvexShape* capsule;
	//Create Collision Shape
	switch( shape )
	{
	case Capsule:
		{
			capsule = new btCapsuleShape( characterWidth, characterHeight );
			break;
		}
	case Cube:
		{
			capsule = new btBoxShape( btVector3( characterWidth, characterHeight, characterDepth ) ); //btCapsuleShape(characterWidth,characterHeight);
			break;
		}
	case Cyl:
		{
			capsule = new btCylinderShape( btVector3( CUBE_HALF_EXTENTS,CUBE_HALF_EXTENTS,CUBE_HALF_EXTENTS ) );
			break;
		}
	case Sph:
		{
			capsule = new btSphereShape( characterWidth );
			break;
		}
	default:
		{
			capsule = new btBoxShape( btVector3( characterWidth, characterHeight, characterDepth ) ); //btCapsuleShape(characterWidth,characterHeight);
			break;
		}
	};
	mGhostObject->setCollisionShape (capsule);
	mGhostObject->setCollisionFlags (btCollisionObject::CF_CHARACTER_OBJECT);

	btScalar stepHeight = btScalar(0.35);
	mCharacter = new btKinematicCharacterController (mGhostObject,capsule,stepHeight);

	//mBody = Object::GetBody();
}

void Player::FixedPosition( D3DXVECTOR3& pos )
{
	Object::FixedPosition( pos );
}

void Player::FixedRotation( float angle )
{
	Object::FixedRotation( angle );
}

void Player::Draw( D3DXMATRIX& lightViewProj, D3DXMATRIX& mat )
{
	//btMotionState* ms = body->getMotionState();
	////btMotionState* ms = mBody->getMotionState();
	//if(ms == NULL)return;

	mMesh->Draw( /*BT2DX_MATRIX( *ms )*/mat, lightViewProj );
}